package frame.userlogin;

import com.alibaba.druid.pool.DruidDataSource;
import frame.managermeau.ManagerHomepageFrame;
import frame.model.SnowflakeIdWorker;
import frame.model.Utils;
import frame.usermeau.UserHomepageFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

public class LoginFrame extends JFrame implements ActionListener {


    JLabel title ;
    JLabel l1;
    JLabel l2;
    JTextField tf_id;
    JPasswordField tf_psd;
    JButton login_btn;
    JButton register_btn;


    DruidDataSource dataSource;


    public LoginFrame(DruidDataSource dataSource) {
        this.dataSource = dataSource;
        this.setTitle("用户登录");
        //设置窗口
        this.setBounds(580,280,400,220);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setLayout(null);

        initComponet();

        this.setVisible(true);
    }

    private void initComponet(){

        title = new JLabel("银行管理系统");
        title.setBounds(150,50,100,30);
        this.add(title);

        l1 = new JLabel("账号");
        l1.setBounds(90,170,100,30);
        this.add(l1);
        tf_id = new JTextField();
        tf_id.setBounds(150,170,150,25);
        this.add(tf_id);

        l2 = new JLabel("密码");
        l2.setBounds(90,230,100,30);
        this.add(l2);
        tf_psd = new JPasswordField();
        tf_psd.setBounds(150,230,150,25);
        this.add(tf_psd);

        login_btn = new JButton("登录");
        login_btn.setBounds(150, 300, 100, 30);
        login_btn.addActionListener(this);
        this.add(login_btn);

        register_btn = new JButton("登录");
        register_btn.setBounds(150, 300, 100, 30);
        register_btn.addActionListener(this);
        this.add(register_btn);

    }

    /**
     * 登录处理，登录成功进行跳转，错误则发出弹窗
     * @param id        登录的id
     * @param passwd    登录的密码
     */
    private void login(String id, String passwd) {

        ResultSet resultSet = null;
        PreparedStatement preparedStatement=null;
        init();
        if((id.length()==11&&Utils.varify("t_user","u_phone",id))||(id.length()==18&&Utils.varify("t_user","u_id_card",id))||(id.length()==20&&Utils.varify("t_user","u_id",id))){
            try {
//                System.out.println("fuck");
                if(id.length()==11){
                    preparedStatement=con.prepareStatement("SELECT * FROM t_user WHERE u_phone =?");
                    preparedStatement.setString(1,id);

                }
                else if(id.length()==18){
                    preparedStatement=con.prepareStatement("SELECT * FROM t_user WHERE u_id_card =?");
                    preparedStatement.setString(1,id);
               }
                else if(id.length()==20){
                    preparedStatement=con.prepareStatement("SELECT * FROM t_user WHERE u_id =?");
                    preparedStatement.setString(1,id);
                }
                resultSet=preparedStatement.executeQuery();
                if(resultSet.isBeforeFirst()==false){
                    JOptionPane.showMessageDialog(null, "不存在该记录");
                    return;
                }
                resultSet.next();
                String u_name=resultSet.getString(2);
                String id_card=resultSet.getString(1);
                String phone_num=resultSet.getString(3);
                String e_mail=resultSet.getString(5)==null?"无":resultSet.getString(5);
                if(resultSet.getString(6).equals(Utils.passwordMD5(passwd))){
                    if(resultSet.getString(7).equals("f")){
                        //跳转用户主界面
                        new UserHomepageFrame(u_name,id_card,phone_num,e_mail);
                        this.dispose();
                    }
                    else{
                        //跳转管理员界面
                        new ManagerHomepageFrame();
                        this.dispose();
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "密码错误");
                    return;
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                ;
            }

            String message = "用户登录成功";
            JOptionPane.showMessageDialog(null, message);
            this.dispose();
        }
        else{
            JOptionPane.showMessageDialog(null, "id不存在");
            return;
        }
        //登录成功与否的返回信息
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    /**
     * 跳转注册界面
     */
    private void register() {
        new RegisterFrame(this.dataSource);
    }


    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        new LoginFrame();
    }


}
